package com.atguigu.lease.web.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.atguigu.lease.common.constant.RedisConstant;
import com.atguigu.lease.common.login.LoginUserHolder;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.service.GraphInfoService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    private final RoomInfoMapper roomInfoMapper;
    private final GraphInfoService graphInfoService;
    private final ApartmentInfoService apartmentInfoService;
    private final FeeValueMapper feeValueMapper;
    private final AttrValueMapper attrValueMapper;
    private final FacilityInfoMapper facilityInfoMapper;
    private final LabelInfoMapper labelInfoMapper;
    private final PaymentTypeMapper paymentTypeMapper;
    private final LeaseTermMapper leaseTermMapper;
    private final BrowsingHistoryServiceImpl browsingHistoryService;
    @Autowired
    private RedisTemplate<String ,Object> redisTemplate;
    @Override
    public IPage<RoomItemVo> pageRoomItemVo(Page page, RoomQueryVo queryVo) {

        return roomInfoMapper.pageRoomItemVo(page,queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailVo(Long id) {
        String key= RedisConstant.APP_ROOM_PREFIX+id;
        RoomDetailVo res = new RoomDetailVo();/*(RoomDetailVo) redisTemplate.opsForValue().get(key);*///查询数据
        if(res==null)
        {
            res=new RoomDetailVo();
            RoomInfo roomInfo = getById(id);
            //bean不简单只是生成还有创建 s只是赋值
            BeanUtils.copyProperties(roomInfo,res);
            //所属公寓信息
            Long apartmentId = roomInfo.getApartmentId();
            ApartmentInfo apartmentInfoServiceById = apartmentInfoService.getById(apartmentId);
            ApartmentItemVo apartmentItemVo = BeanUtil.copyProperties(apartmentInfoServiceById, ApartmentItemVo.class);
            res.setApartmentItemVo(apartmentItemVo);
            //图片列表
            List<GraphInfo> graphInfos = graphInfoService.lambdaQuery()
                    .eq(GraphInfo::getItemType, ItemType.ROOM)
                    .eq(GraphInfo::getItemId, id)
                    .list();
            List<GraphVo> graphVos = BeanUtil.copyToList(graphInfos, GraphVo.class);
            res.setGraphVoList(graphVos);
            //属性信息列表
            List<AttrValueVo> roomAttrValues = attrValueMapper.getRoomAttrValue(id);
            res.setAttrValueVoList(roomAttrValues);
            //杂费列表
            List<FeeValueVo> feeValueVos=feeValueMapper.getFeeValueVo(apartmentInfoServiceById.getId());
            res.setFeeValueVoList(feeValueVos);
            //配套信息列表
            List<FacilityInfo> facilityInfos=facilityInfoMapper.getFacilityInfo(id);
            res.setFacilityInfoList(facilityInfos);
            // 标签信息列表
            List<LabelInfo> labelInfoList=labelInfoMapper.getlabelInfoList(id);
            res.setLabelInfoList(labelInfoList);
            //支付方式列表
            List<PaymentType> paymentTypeList=paymentTypeMapper.getPaymentType(id);
            res.setPaymentTypeList(paymentTypeList);
            //租期列表
            List<LeaseTerm> leaseTermList=leaseTermMapper.getLeaseTerm(id);
            res.setLeaseTermList(leaseTermList);
            redisTemplate.opsForValue().set(key,res);
        }
        //保存浏览历史
        /*BrowsingHistory history=new BrowsingHistory();
        browsingHistoryService.saveHistory(LoginUserHolder.getLoginUser().getUserId(),id);*/
        return res;
    }

    @Override
    public IPage<RoomItemVo> getIdpage(Page<RoomItemVo> page, Long id) {
        return roomInfoMapper.getIdpage(page,id);
    }
}




